package com.scqzy.shengchunqiang.sword2offer.day09;

/**
 * @Description: 剑指 Offer 42. 连续子数组的最大和
 * @Author 盛春强
 * @Date 2021/8/16 10:31
 */
public class MaxSubArray {
    public int maxSubArray(int[] nums) {
        int max = nums[0];
        int[] dp = new int[nums.length];
        dp[0] = nums[0];
        for (int i = 1; i < nums.length; i++) {
            // [-2, 1, -2, 4, 3, 5, 6, 1, 5]
            dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
            max = Math.max(max, dp[i]);
        }
        return max;
    }

    public static void main(String[] args) {
        MaxSubArray maxSubArray = new MaxSubArray();
        int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        int i = maxSubArray.maxSubArray(nums);
        System.out.println(i);
    }
}
